package com.jinlejia.bbs.widget.mp3;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.google.android.exoplayer2.ui.TimeBar;
import com.google.android.exoplayer2.util.Util;
import com.jinlejia.bbs.adapter.BaseLoadAdapter;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.databinding.BbsMp3ViewLayoutBinding;

/**
 * @author 王进 on 2019/4/12.
 */

public abstract class Mp3AdapterHelper<B> extends BaseLoadAdapter<B> implements AudioController.AudioControlListener {
    private final AudioController audioController;
    protected final RecyclerView recyclerView;
    public Mp3AdapterHelper(Context context,AudioController audioController,RecyclerView recyclerView) {
        super(context);
        this.audioController = audioController;
        this.audioController.setOnAudioControlListener(this);
        this.recyclerView = recyclerView;
    }

    protected abstract SoundBean getMp3SoundBean(int position);
    protected abstract BbsMp3ViewLayoutBinding getMp3Binding(int position);
    protected final void initMp3View(final BbsMp3ViewLayoutBinding binding, final SoundBean soundBean, final int currPosition){
        if (soundBean.isPlay()){
            binding.mp3PauseButton.setVisibility(View.VISIBLE);
            binding.mp3PlayButton.setVisibility(View.GONE);
        }else {
            binding.mp3PlayButton.setVisibility(View.VISIBLE);
            binding.mp3PauseButton.setVisibility(View.GONE);
        }
        binding.mp3PauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean b = audioController.getPosition() == currPosition;
                System.out.println("=====>Pause"+audioController.getPosition()+":"+currPosition);
                if (b){ //如果要暂停的是正在播放的，则暂停
                    audioController.onPause();
                }else {
                    System.out.println("CCC");
                }
            }
        });
        binding.mp3PlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initStatus(audioController.getPosition(),currPosition,binding,soundBean);
                audioController.onPrepare(soundBean.getUrl());
                System.out.println("=====>Play:"+audioController.getPosition()+":"+currPosition);
                audioController.onStart(currPosition);
            }
        });
        binding.mp3StartTime.setText(soundBean.getStartTime());
        binding.mp3EndTime.setText(soundBean.getEndTime());
        binding.exoProgress.setDuration(soundBean.getDuration());
        binding.exoProgress.setPosition(0);
        binding.exoProgress.addListener(new TimeBar.OnScrubListener(){

            @Override
            public void onScrubStart(TimeBar timeBar, long position) {

            }

            @Override
            public void onScrubMove(TimeBar timeBar, long position) {
                if (audioController.getPosition() == currPosition) {
                    binding.mp3StartTime.setText(Util.getStringForTime(audioController.getFormatBuilder(),
                            audioController.getFormatter(), position));
                } else {
                    timeBar.setPosition(0);
                }
            }

            @Override
            public void onScrubStop(TimeBar timeBar, long position, boolean canceled) {
                if (audioController!=null){
                    if (audioController.getPosition()==currPosition){
                        audioController.seekToTimeBarPosition(position);
                    }else {
                        timeBar.setPosition(0);
                    }
                }
            }
        });
    }

    /**
     *  @param playPosition 正在播放的索引
     * @param clickPosition 用户点击的索引
     * @param clickBinding 用户将要播放的UI
     * @param clickBean 用户将要播放的数据
     */
    private void initStatus(int playPosition, int clickPosition, BbsMp3ViewLayoutBinding clickBinding, SoundBean clickBean) {
        SoundBean oldSoundBean = getMp3SoundBean(playPosition);
        if (oldSoundBean==null)return;
        oldSoundBean.setStartTime("00:00");
        oldSoundBean.setPlay(false);
        BbsMp3ViewLayoutBinding oldMp3Binding = getMp3Binding(playPosition);
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        if (playPosition>=layoutManager.findFirstVisibleItemPosition()
                && playPosition <= layoutManager.findLastVisibleItemPosition()){ //正在播放的界面，正在显示中
            oldMp3Binding.exoProgress.setPosition(0);
            oldMp3Binding.exoProgress.setBufferedPosition(0);
            oldMp3Binding.mp3StartTime.setText(oldSoundBean.getStartTime());
            oldMp3Binding.mp3PlayButton.setVisibility(View.VISIBLE);
            oldMp3Binding.mp3PauseButton.setVisibility(View.GONE);

            clickBinding.mp3PlayButton.setVisibility(View.GONE);
            clickBinding.mp3PauseButton.setVisibility(View.VISIBLE);
        }else {
            notifyItemChanged(playPosition);
        }
    }

    @Override
    public void setCurPositionTime(int position, long curPositionTime) {
        BbsMp3ViewLayoutBinding binding = getMp3Binding(position);
        if (binding!=null)
        binding.exoProgress.setPosition(curPositionTime);
    }

    @Override
    public void setDurationTime(int position, long durationTime) {
        BbsMp3ViewLayoutBinding binding = getMp3Binding(position);
        if (binding!=null)
        binding.exoProgress.setDuration(durationTime);
    }

    @Override
    public void setBufferedPositionTime(int position, long bufferedPosition) {
        BbsMp3ViewLayoutBinding binding = getMp3Binding(position);
        if (binding!=null)
        binding.exoProgress.setBufferedPosition(bufferedPosition);
    }

    @Override
    public void setCurTimeString(int position, String curTimeString) {
        BbsMp3ViewLayoutBinding binding = getMp3Binding(position);
        if (binding!=null)
            binding.mp3StartTime.setText(curTimeString);
        SoundBean soundBean = getMp3SoundBean(position);
        if (soundBean==null)return;
        soundBean.setStartTime(curTimeString);
    }

    @Override
    public void isPlay(int position, boolean isPlay) {
        SoundBean soundBean = getMp3SoundBean(position);
        if (soundBean==null)return;
        soundBean.setPlay(isPlay);
        BbsMp3ViewLayoutBinding binding = getMp3Binding(position);
        if (binding!=null)
        if (isPlay){
            binding.mp3PlayButton.setVisibility(View.GONE);
            binding.mp3PauseButton.setVisibility(View.VISIBLE);
        }else {
            binding.mp3PlayButton.setVisibility(View.VISIBLE);
            binding.mp3PauseButton.setVisibility(View.GONE);
        }
    }

    @Override
    public void setDurationTimeString(int position, String durationTimeString) {
        BbsMp3ViewLayoutBinding binding = getMp3Binding(position);
        if (binding!=null)
        binding.mp3EndTime.setText(durationTimeString);
        SoundBean soundBean = getMp3SoundBean(position);
        soundBean.setEndTime(durationTimeString);
    }
}
